package com.arong.swing.db.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.arong.swing.db.JDBCUtils;
import com.arong.swing.db.entity.CrBidPerson;
import com.arong.swing.db.entity.PagingInfo;
import com.arong.swing.db.entity.PagingResult;
import com.arong.swing.exception.AppException;
import com.arong.swing.exception.SysException;
import com.arong.swing.util.StringUtil;
public class CrBidPersonSDAO  extends BaseDAO {
	public CrBidPersonSDAO(){
		this(DB_CHECKRECORD);
	}
	public CrBidPersonSDAO(String dbFile){
		super(dbFile);
	}
	public CrBidPersonSDAO(JDBCUtils jdbcUtils){
		super(jdbcUtils);
	}
	public String getCreateTableSql(){
		return "CREATE TABLE IF NOT EXISTS "+
				"CR_BID_PERSON("+
						 " ID  VARCHAR2(32) PRIMARY KEY,"+
						 " HCJL_ID VARCHAR2(32),"+
						 " CHECKEVENT_ID VARCHAR2(32),"+
						 " TASK_NAMES VARCHAR2(512),"+
						 " TASK_ID VARCHAR2(512),"+
						 " PACKAGE_FIRST_IDS VARCHAR2(512),"+
						 " PACKAGE_VER_IDS VARCHAR2(512),"+
						 " PACKAGE_USER VARCHAR2(512),"+
						 " PACKAGE_MOBILE VARCHAR2(512),"+
						 " SFZH VARCHAR2(32) ,"+
						 " PCKSSJ DATE,"+
						 " RYLB VARCHAR2(32),"+
						 " DESC_PERSON VARCHAR2(1000)"+
						 ")";
		
	}
	
	/**
	 * 插入
	 * @param entity
	 * @return
	 * @throws SysException, AppException
	 */
	public  int insert(CrBidPerson entity) throws SysException,AppException {
		int i=0;
		try {
		 		    if (StringUtil.isBlank(entity.getId())) {
				entity.setId(getSeqValue());
			}
		 			i= jdbcUtils.insert(entity);
		} catch (SQLException e) {
			throw new SysException("add error", e);
		}
		return i;
	}

	/**
	 * 修改
	 * @param entity
	 * @return
	 * @throws SysException, AppException
	 */
	public  int update(CrBidPerson entity) throws SysException,AppException {
		int i=0;
		try {
			i=jdbcUtils.update(entity, "id");
		} catch (SQLException e) {
			throw new SysException("update error",e);
		}
		return i;
	}

	/**
	 * 删除
	 * @param entity
	 * @return
	 * @throws SysException, AppException
	 */
	public  int delete(CrBidPerson entity) throws SysException,AppException {
		int i = 0;
		try {
			i=jdbcUtils.execute("DELETE FROM CR_BID_PERSON WHERE ID='"
					    +entity.getId()
		    		    +"'"
			);
		} catch (SQLException e) {
			throw new SysException("delete error", e);
		}
		return i;
	}

	/**
	 * 分页查询
	 * @param sql
	 * @param pagingInfo
	 * @param entity
	 * @return 
	 * @throws SysException, AppException
	 */
	public  PagingResult<CrBidPerson> queryList(PagingInfo pagingInfo, CrBidPerson entity)
			throws SysException,AppException {
		PagingResult<CrBidPerson> res=null;
	    try {
			res= jdbcUtils.queryList(getSql(entity), CrBidPerson.class,
				pagingInfo, getParams(entity));
		} catch (SQLException e) {
			throw new SysException("queryList分页查询 error",e);
		}
		return   res;
	}

	/**
	 * 查询 集合
	 * @param entity
	 * @return
	 * @throws SysException, AppException
	 */
	public   List<CrBidPerson> queryList(CrBidPerson entity) throws SysException,AppException {
	    List<CrBidPerson> res=null;
	    try {
			res= jdbcUtils.queryList(getSql(entity), CrBidPerson.class,
			getParams(entity));
		} catch (SQLException e) {
			throw new SysException("queryList集合查询 error",e);
		}
		return   res;
	}

	/**
	 * 查询对象
	 * @param entity
	 * @return
	 * @throws SysException, AppException
	 */
	public  CrBidPerson queryBean(CrBidPerson entity) throws SysException,AppException {
		CrBidPerson res=null;
	    try {
			res=  jdbcUtils.queryBean(getSql(entity), CrBidPerson.class,
				getParams(entity));
		} catch (SQLException e) {
			throw new SysException("queryBean对象查询 error",e);
		}
		return   res;
	}

	/**
	 * 获取查询条件
	 * @return
	 */
	private Object[] getParams(CrBidPerson crBidPerson) {
		List<Object> qryList = new ArrayList<Object>();
		if (crBidPerson.getId() != null) {
			qryList.add(crBidPerson.getId());
		}
		if (crBidPerson.getHcjlId() != null) {
			qryList.add(crBidPerson.getHcjlId());
		}
		if (crBidPerson.getCheckeventId() != null) {
			qryList.add(crBidPerson.getCheckeventId());
		}
		
		if (crBidPerson.getTaskNames() != null) {
			qryList.add(crBidPerson.getTaskNames());
		}
		if (crBidPerson.getPackageUser() != null) {
			qryList.add(crBidPerson.getPackageUser());
		}
		if (crBidPerson.getPackageMobile() != null) {
			qryList.add(crBidPerson.getPackageMobile());
		}
		if (crBidPerson.getSfzh() != null) {
			qryList.add(crBidPerson.getSfzh());
		}
		if (crBidPerson.getPckssj() != null) {
			qryList.add(crBidPerson.getPckssj());
		}
		if (crBidPerson.getRylb() != null) {
			qryList.add(crBidPerson.getRylb());
		}
		if (crBidPerson.getDescPerson() != null) {
			qryList.add(crBidPerson.getDescPerson());
		}
		return jdbcUtils.setQryList(qryList);
	}

	/**
	 * 获取sql 
	 * @return
	 */
	private String getSql(CrBidPerson crBidPerson) {
		StringBuffer sql = new StringBuffer("SELECT ");
		sql.append(" a.ID,a.HCJL_ID,a.CHECKEVENT_ID,a.TASK_NAMES,a.TASK_ID,a.PACKAGE_FIRST_IDS,a.PACKAGE_VER_IDS,a.PACKAGE_USER,a.PACKAGE_MOBILE,a.SFZH,a.PCKSSJ,a.RYLB,a.DESC_PERSON");
		sql.append(" from CR_BID_PERSON a where 1=1");
		if (crBidPerson.getId() != null) {
			sql.append(" and ID=?");
		}
		if (crBidPerson.getHcjlId() != null) {
			sql.append(" and HCJL_ID=?");
		}
		if (crBidPerson.getCheckeventId() != null) {
			sql.append(" and CHECKEVENT_ID=?");
		}
		
		if (crBidPerson.getTaskNames() != null) {
			sql.append(" and TASK_NAMES=?");
		}
		if (crBidPerson.getPackageUser() != null) {
			sql.append(" and PACKAGE_USER=?");
		}
		if (crBidPerson.getPackageMobile() != null) {
			sql.append(" and PACKAGE_MOBILE=?");
		}
		if (crBidPerson.getSfzh() != null) {
			sql.append(" and SFZH=?");
		}
		if (crBidPerson.getPckssj() != null) {
			sql.append(" and PCKSSJ=?");
		}
		if (crBidPerson.getRylb() != null) {
			sql.append(" and RYLB=?");
		}
		if (crBidPerson.getDescPerson() != null) {
			sql.append(" and DESC_PERSON=?");
		}
		return sql.toString();
	}
}

